--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function EditBiddingSystemFrame:Initialize(app)
    
    --Save the app
    self.app = app
    
    --Setup the add dew drop
    self.addSystemDrop = AceLibrary("Dewdrop-2.0")
    self.systemTab = {
        handler=self,
        type = "group",
        args = {
            h1 = {
                type="header",
                name="Available systems:",
                order=1,
            },
            h2 = {
                type = "header",
                order=100,
            },
            close = {
                type="execute",
                name="Close",
                desc="Close this menu.",
                func= function() self.addSystemDrop:Close() end,
                order=1000,
            },
        },
    }
    
    local order = 10
    for name, system in pairsByKeys(BiddingSystems) do
        self.systemTab.args[name] = {
            type = "execute",
            name = name,
            desc = (system.instructions or ""),
            func = function()
                table.insert(self.biddingsystems, system)
                self:updateView()
                ViewItemsFrame:updateView()
            end,
            order = order,
        }
        order = order + 1
    end
    
    --Do some basic setup of our table
    EditBiddingSystemTable:setWidths({50,132.5,172.5})
    EditBiddingSystemTable:setFields({"Phase","System name","Instructions"})
    EditBiddingSystemTable:registerSelectionCallback(function(row, selected) self:handleRowSelected(row, selected) end)
    self:handleRowSelected(nil, false)
    
    --Just close us when something changes
    self.app:registerEvent("BrownieHelper_LootChanged", function() self:Hide() end)
    self.app:registerEvent("BrownieHelper_ModeChanged", function() self:Hide() end)
    
end

function EditBiddingSystemFrame:editBiddingSystems(itemInfo, closeCallback)
    self.closeCallback = closeCallback
    self.biddingsystems = itemInfo.biddingsystems
    self:Show()
    self:updateView()
end

function EditBiddingSystemFrame:updateView()

    EditBiddingSystemTable:clearRows()
    for i,system in pairs(self.biddingsystems) do
        EditBiddingSystemTable:addRow( {
            ["Phase"] = i,
            ["System name"] = system.name,
            ["Instructions"] = (system.instructions or ""),
        })
    end
    
end

function EditBiddingSystemFrame:handleRowSelected(row,selected)
    if selected==true then
        EditBiddingSystemRemoveSystemButton:Enable()
    else
        EditBiddingSystemRemoveSystemButton:Disable()
    end
end

function EditBiddingSystemFrame:OnAddSystem()
    self.addSystemDrop:Open(EditBiddingSystemAddSystemButton, 'children', function() self.addSystemDrop:FeedAceOptionsTable(self.systemTab) end)
end

function EditBiddingSystemFrame:OnRemoveSystem()
    for i,row in pairs(EditBiddingSystemTable:getSelections()) do
        table.remove(self.biddingsystems, tonumber(row.Phase))
    end
    self:updateView()
    ViewItemsFrame:updateView()
end

function EditBiddingSystemFrame:OnClose()
    if self.closeCallback then self.closeCallback() end
    self:Hide()
end
